﻿using LiveCharts;
using LiveCharts.Definitions.Series;
using LiveCharts.Wpf;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media;

namespace Wpf_Examples.Models
{
    public class ChartViewModel
    {
        public ObservableCollection<ISeriesView> Series { get; set; }
        public ObservableCollection<Axis> XAxes { get; set; }
        public ObservableCollection<Axis> YAxes { get; set; }

        public ChartViewModel()
        {
            Series = new ObservableCollection<ISeriesView>();
            XAxes = new ObservableCollection<Axis>
        {
            new Axis
            {
                Title = "X Axis",
                MinValue = 0,
                MaxValue = 10
            }
        };
            YAxes = new ObservableCollection<Axis>();

            // 初始化一些Y轴和数据系列
            AddYAxis("Y Axis 1");
            AddYAxis("Y Axis 2");

            AddSeries("Series 1", "Y Axis 1", new[] { 1.0, 3.0, 5, 7, 9 }, Brushes.Red);
            AddSeries("Series 2", "Y Axis 1", new[] { 2, 4.0, 6, 8, 10 }, Brushes.Blue);
            AddSeries("Series 3", "Y Axis 2", new[] { 1.5, 3.5, 5.5, 7.5, 9.5 }, Brushes.Green);
        }

        public void AddYAxis(string title)
        {
            YAxes.Add(new Axis
            {
                Title = title,
                MinValue = 0,
                MaxValue = 10
            });
        }

        public void RemoveYAxis(Axis axis)
        {
            if (YAxes.Contains(axis))
            {
                YAxes.Remove(axis);

                // 移除与该Y轴关联的所有数据系列
                var seriesToRemove = Series.Where(s => s == axis).ToList();
                foreach (var series in seriesToRemove)
                {
                    Series.Remove(series);
                }
            }
        }

        public void AddSeries(string name, string yAxisTitle, double[] data, Brush color)
        {
            var yAxis = YAxes.FirstOrDefault(y => y.Title == yAxisTitle);
            //if (yAxis != null)
            //{
            //    var lineSeries = new LineSeries
            //    {
            //        Title = name,
            //        Values = new ChartValues<double>(data),
            //        LineSmoothness = 0.2, // 平滑曲线
            //        PointGeometry = null, // 不显示点
            //        Stroke = color,
            //        YAxis = yAxis
            //    };

            //    Series.Add(lineSeries);
            //}

        }
    }
}
